package com.uh.calculation.inter.imp;


import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.operator.*;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.GetOrganization;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.summary.enums.RuleEnum;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
public class FuwuImp implements Warn {
    private JdbcDao jdbcDao;

    /**
     * 业务变量配置
     *
     * @param warnJiSuan
     * @return
     */
    //////////////////////////////////////////////////////////////修改
    //修改频次
    private String xgpc = "";

    //1-本月 营业收入修改比例超过15%，计预警因子50。
    //年报营业收入修改比例超过15%，计预警因子50。（此条仅用于4月年报出来时，但一直触发到该企业被核查或检查为止）
    private String  xgfd="[{dimension:\"A\",field:\"$NEW_VALUE;$NEW_VALUE#m\",formulaBeans:[{formula:\"($NEW_VALUE@$NEW_VALUE#m)/$NEW_VALUE#m>0.15|50\"}]}]";


    private String  xgcs="[{dimension:\"A\",field:\"$NEW_VALUE\",formulaBeans:[{formula:\"$NEW_VALUE>3|50\"}]}]";


    //////////////////////////////////////////////////////////////异常
    //运营状态
    private String yxzt = "[{dimension:\"B\",field:\"201-1_M62\",formulaBeans:[{formula:\"201-1_M62=2|20\"},{formula:\"201-1_M62=4|20\"},{formula:\"201-1_M62=5|20\"},{formula:\"201-1_M62=6|20\"},{formula:\"201-1_M62=7|20\"}]}]";
    //被举报
    private String bjb = "";
    //填表人变动
    private String tbr = "";
    //小升规  M10为行业代码  M32为营业收入
    private String xsg = "[{dimension:\"B\",field:\"201-1_M10;F103_M33\",formulaBeans:[{formula:\"201-1_M10=53&F103_M33<=24000|50\"},{formula:\"201-1_M10=54&F103_M33<=24000|50\"},{formula:\"201-1_M10=56&F103_M33<=24000|50\"},{formula:\"201-1_M10=57&F103_M33<=24000|50\"},{formula:\"201-1_M10=58)&F103_M33<=24000|50\"},{formula:\"201-1_M10=59&F103_M33<=24000|50\"},{formula:\"201-1_M10=60&F103_M33<=24000|50\"},{formula:\"201-1_M10=63&F103_M33<=24000|50\"},{formula:\"201-1_M10=64&F103_M33<=24000|50\"},{formula:\"201-1_M10=65&F103_M33<=24000|50\"},{formula:\"201-1_M10=71&F103_M33<=12000|50\"},{formula:\"201-1_M10=72&F103_M33<=12000|50\"},{formula:\"201-1_M10=73&F103_M33<=12000|50\"},{formula:\"201-1_M10=74&F103_M33<=12000|50\"},{formula:\"201-1_M10=75&F103_M33<=12000|50\"},{formula:\"201-1_M10=83&F103_M33<=12000|50\"},{formula:\"201-1_M10=7020&F103_M33<=12000|50\"},{formula:\"201-1_M10=7030&F103_M33<=12000|50\"},{formula:\"201-1_M10=7040&F103_M33<=12000|50\"},{formula:\"201-1_M10=7090&F103_M33<=12000|50\"},{formula:\"201-1_M10=80&F103_M33<=5100|50\"},{formula:\"201-1_M10=81&F103_M33<=5100|50\"},{formula:\"201-1_M10=82&F103_M33<=5100|50\"},{formula:\"201-1_M10=85&F103_M33<=5100|50\"},{formula:\"201-1_M10=86&F103_M33<=5100|50\"},{formula:\"201-1_M10=87&F103_M33<=5100|50\"},{formula:\"201-1_M10=88&F103_M33<=5100|50\"},{formula:\"201-1_M10=89&F103_M33<=5100|50\"},{formula:\"201-1_M10=90&F103_M33<=5100|50\"}]}]";
    //////////////////////////////////////////////////////////////调整
    //上一年度第12月的1-本月营业收入与本年营业收入比值为10%，计50分，每低10%一个百分点，加2分。
//    private String tz1 = "[{\"dimension\":\"C\",\"field\":\"F203_M11;F103_M33;\",\"formulaBeans\":[{\"formula\":\"(F203_M11/F103_M33)=0.1|50\"},{\"formula\":\"(F203_M11/F103_M33)<0.1|50+(0.1-F203_M11/F103_M33)/0.01*2\"}]}]";
    private String tz1 = "[{\"dimension\":\"C\",\"field\":\"F203_M11;F203_M11#m;F103_M33;\",\"formulaBeans\":[{\"formula\":\"((F203_M11-F203_M11#m)/F103_M33)=0.1|50\"},{\"formula\":\"((F203_M11-F203_M11#m)/F103_M33)<0.1|50+(0.1-(F203_M11-F203_M11#m)/F103_M33)/0.01*2\"}]}]";
    //上一年度第12月的1-本月营业收入与本年营业收入比值为40%，计50分，每高40%一个百分点，加2分
//    private String tz2 = "[{\"dimension\":\"C\",\"field\":\"F203_M11;F103_M33;\",\"formulaBeans\":[{\"formula\":\"(F203_M11/F103_M33)=0.4|50\"},{\"formula\":\"(F203_M11/F103_M33)>0.4|50+(F203_M11/F103_M33-0.4)/0.01*2\"}]}]";
    private String tz2 = "[{\"dimension\":\"C\",\"field\":\"F203_M11;F203_M11#m;F103_M33;\",\"formulaBeans\":[{\"formula\":\"((F203_M11-F203_M11#m)/F103_M33)=0.4|50\"},{\"formula\":\"((F203_M11-F203_M11#m)/F103_M33)>0.4|50+((F203_M11-F203_M11#m)/F103_M33-0.4)/0.01*2\"}]}]";
    private String tz3 = "";
    private String tz4 = "";
    //////////////////////////////////////////////////////////////增速
    //1-本月 营业收入同比增速增长或下降为20%，计150分。每超过20%一个百分点，加5分。
    private String zs1 = "[{dimension:\"D\",field:\"F203_M11;F203_M12\",formulaBeans:[{formula:\"((F203_M11@F203_M12)/F203_M12)>=0.2&((((F203_M11@F203_M12)/F203_M12)-0.2)*100)*5+150>=300|300\"},{formula:\"((F203_M11@F203_M12)/F203_M12)>=0.2&((((F203_M11@F203_M12)/F203_M12)-0.2)*100)*5+150<300|((((F203_M11@F203_M12)/F203_M12)-0.2)*100)*5+150\"}]}]";
    //引入当月营业收入概念，当月营业收入=1-本月营业收入减去上一个月的1-本月营业收入。
    //当月营业收入同比增速增长或下降为20%，计150分。每超过20%一个百分点，加5分。
    private String zs2 = "[{dimension:\"D\",field:\"F203_M11;F203_M11#m\",formulaBeans:[{formula:\"((F203_M11@F203_M11#m)/F203_M11#m)>0.2&((((F203_M11@F203_M11#m)/F203_M11#m)-0.2)*100)*5+150>=300|300\"},{formula:\" ((F203_M11 @F203_M11#m) / F203_M11#m) >=0.2& ((((F203_M11 @F203_M11#m) / F203_M11#m) - 0.2) * 100) * 5 + 150<300|((((F203_M11 @F203_M11#m) / F203_M11#m) - 0.2) * 100) * 5 + 150 \"}]}]";
    //1-本月净服务收入（F203表的340）同比增长或下级20%，计100分，每超过20%一个百分点，加2分。
    private String zs3 = "[{dimension:\"D\",field:\"F203_M13;F203_M14\",formulaBeans:[{formula:\"((F203_M13@F203_M14)/F203_M14)>=0.2&((((F203_M13@F203_M14)/F203_M14)-0.2)*100)*2+100>=200|200\"},{formula:\" ((F203_M13 @F203_M14) / F203_M14) >= 0.2 & ((((F203_M13 @F203_M14) / F203_M14) - 0.2) * 100) * 2 + 100 < 200 | ((((F203_M13 @F203_M14) / F203_M14) - 0.2) * 100) * 2 + 100\"}]}]";
    //当月净服务收入同比或环比增速增长或下降为20%，计100分。每超过20%一个百分点，加2分 (未找打当月净服务)
    //private  String  zs4="[{field: \"F203_M13\",threshold: \"]20=150\",zsType:\"3\",greater:\"20=5\",isAbs:\"1\"}]";
    //1-本月营业利润（F203表的323）同比增长或下级20%，计100分，每超过20%一个百分点，加2分
    private String zs5 = "[{dimension:\"D\",field:\"F203_M41;F203_M42\",formulaBeans:[{formula:\"((F203_M41@F203_M42)/F203_M42)>=0.2&((((F203_M41@F203_M42)/F203_M42)-0.2)*100)*2+100>=200|200\"},{formula:\"((F203_M41@F203_M42)/F203_M42)>=0.2&((((F203_M41@F203_M42)/F203_M42)-0.2)*100)*2+100<200|((((F203_M41@F203_M42)/F203_M42)-0.2)*100)*2+100\"}]}]";
    //当月营业利润同比或环比  增速增长或下降为20%，计150分。每超过20%一个百分点，加2分  (未找到当月营业利润)
    //private  String  zs6="[{field: \"F203_M13\",threshold: \"]20=150\",zsType:\"3\",greater:\"20=5\",isAbs:\"1\"}]";


    //////////////////////////////////////////////////////////////匹配
    //private  String  pp1="f203_m3@f203_m5@]115=100@#1&2@#85==1@#-85&2";

    private String pp1 = "";


    public WarnJiSuanBean warnJiSuan = null;


    //通用表
    List<Map<String, Object>> F203ls = null;

    List<Map<String, Object>> F103ls = null;

    List<Map<String, Object>> F201_1ls = null;

    List<Map<String, Object>> HJ_ls = null;
    List<Map<String, Object>> HJN_ls = null;






    @Override
    public Integer jisuan(WarnJiSuanBean warnJiSuan) {

        System.out.println("进入计算："+warnJiSuan.getSurveyobjectid());
        this.warnJiSuan = warnJiSuan;
        this.jdbcDao = warnJiSuan.getJdbcDao();
        GetOrganization getOrganization = new GetOrganization(jdbcDao);
        /**
         * 当前你所用所有list一次取出
         */
        F203ls = getOrganization.getFieldName("F203", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        F201_1ls = getOrganization.getFieldName("201-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        F103ls = getOrganization.getFieldName_TJRY("F103", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid(), warnJiSuan.getLastTime());


        HJ_ls =  getOrganization.gethj("et_8600000002021110107721060", warnJiSuan.getSurveyobjectid());
        //年报
        HJN_ls =  getOrganization.gethj("et_8600000002021110107721004", warnJiSuan.getSurveyobjectid());

        warnJiSuan.setF201_1ls(F201_1ls);



        String year_is_month = getOrganization.gethc(warnJiSuan.getSurveyobjectid());//核查时间
        warnJiSuan.setYear_is_month(year_is_month);


        HjOperator hjOperator = new HjOperator(jdbcDao, warnJiSuan);
        List<OperatorVal> list = JSONObject.parseArray(xgfd, OperatorVal.class);
        //修改一
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
        hjOperator.jisuan(HJ_ls, list,"1-本月;营业收入;千元");

        warnJiSuan.setIs_hc(true);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_EDIT_FREQUENCY_FACTOR_YEAR_DATA.getCode());
        hjOperator.jisuan(HJN_ls, list,"本年;营业收入;千元");


        list = JSONObject.parseArray(xgcs, OperatorVal.class);
        HjNumOperator  hjNumOperator = new HjNumOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
        hjNumOperator.jisuan(HJN_ls, list,"1-本月;营业收入;千元");





        //启动同比增长算子
        ZsOperator zsOperator = new ZsOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(zs1, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_GROW_FACTOR_DATA.getCode());//风险因子类型
        zsOperator.jisuan(F203ls, list);


        list = JSONObject.parseArray(zs3, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_GROW_FACTOR_DATA1.getCode());//风险因子类型
        zsOperator.jisuan(F203ls, list);

        list = JSONObject.parseArray(zs5, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_GROW_FACTOR_DATA2.getCode());//风险因子类型
        zsOperator.jisuan(F203ls, list);

        //启动环比增长算子
        ZsHuanbiOperator zsHuanbiOperator = new ZsHuanbiOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(zs2, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_GROW_FACTOR_DATA.getCode());//风险因子类型
        zsHuanbiOperator.jisuan(F203ls, list);

        JtYOperator jtYOperator = new JtYOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(tz1, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_ADJUSTMENT_FACTOR_YEAR_DATA.getCode());//风险因子类型
        jtYOperator.jisuan(F103ls, F203ls, list);

        list = JSONObject.parseArray(tz2, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setYear_is_month(year_is_month);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_ADJUSTMENT_FACTOR_YEAR_DATA.getCode());
        jtYOperator.jisuan(F103ls, F203ls, list);

        //运营状态
        YyztOperator yyztOperator = new YyztOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(yxzt, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_ABNORMAL_MANAGE_FACTOR_DATA.getCode());//风险因子类型
        yyztOperator.jisuanYyzt(F201_1ls, list);

        //小升规多字段 算子
        list = JSONObject.parseArray(xsg, OperatorVal.class);
        warnJiSuan.setIs_hc(true);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_ABNORMAL_MANAGE_FACTOR_DATA.getCode());//风险因子类型
        jtYOperator.jisuan(F103ls, F201_1ls, list);


        //启动统计人员变更计算
        TjryOperator tjryOperator = new TjryOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.SERVICE_ABNORMAL_REPORT_FACTOR_DATA.getCode());//风险因子类型
        tjryOperator.jisuan(F103ls, "M5", "B");

        return null;
    }
}
